﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThucTapOnsoft.Models;

namespace ThucTapOnsoft.Controllers
{
    public class AdminController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Admin";
            return View();
        }

        nuochoamypham_dbEntities3 db = new nuochoamypham_dbEntities3();
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection, MONHOC mon)
        {
            string stenmon = collection["txtTenMon"];
            int sOrd = int.Parse(collection["txtOrd"]);
            mon.Name = stenmon;
            mon.Ord = sOrd;
            db.MONHOCs.Add(mon);
            db.SaveChanges();
            return View();
        }

        public ActionResult Edit(int id)
        {
            var mon = db.MONHOCs.First(m => m.Id == id);
            return View(mon);
        }
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection, MONHOC mon)
        {
            int iId = int.Parse(collection["txtId"]);
            string stenmon = collection["txtTenMon"];
            int sOrd = int.Parse(collection["txtOrd"]);

            mon.Id = iId;
            mon.Name = stenmon;
            mon.Ord = sOrd;
            UpdateModel(mon);
            db.SaveChanges();
            return RedirectToAction("Edit");
        }

    }
}
